<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <title>添加</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="stylesheet" href="/layuiadmin/layui/css/layui.css" media="all"/>
    <link rel="stylesheet" href="/layuiadmin/style/admin.css" media="all"/>
    <script src="/layuiadmin/layui/layui.js"></script>
</head>
<body style="background-color:#fff">

<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="" class="layui-form layui-form-pane" lay-filter="layui-form-pane" id="sbumitForm">
            <div class="layui-row layui-col-space10 layui-form-item">
                <div class="layui-col-lg6">
                    <label class="layui-form-label">名称：</label>
                    <div class="layui-input-block">
                        <input type="text" name="name"  placeholder="道具名称" lay-verify="required" autocomplete="off" class="layui-input" value=""/>
                    </div>
                </div>
            </div>

            <div class="layui-row layui-col-space10 layui-form-item">
                <label class="layui-form-label">图标</label>
                <div class="layui-input-inline">
                    <input name="icon" lay-verify="required" id="icon" autocomplete="off"  placeholder="请上传图片或输入地址" value="" class="layui-input" >
                </div>
                <div class="layui-input-inline layui-btn-container" style="width: auto;">
                    <button type="button" class="layui-btn layui-btn-primary" id="Upload">
                        <i class="layui-icon">&#xe67c;</i>上传图片
                    </button>
                    <span class="layui-btn layui-btn-primary" onclick="icon_view()">查看图片</span >
                </div>
            </div>

            <div class="layui-row layui-col-space10 layui-form-item">
                <div class="layui-col-lg6">
                    <label class="layui-form-label">功能属性：</label>
                    <div class="layui-input-block">
                        <select name="type" lay-verify="required">
                            <option value="">请选择</option>
                            {volist name="type_cate" id="vo"}
                            <option value="{$key}">{$vo}</option>
                            {/volist}
                        </select>
                    </div>
                </div>
            </div>

            <div class="layui-row layui-col-space10 layui-form-item">
                <div class="layui-col-lg6">
                    <label class="layui-form-label">道具说明：</label>
                    <div class="layui-input-block">
                        <input type="text" name="remark"  placeholder="道具说明"  autocomplete="off" class="layui-input" value="" />
                    </div>
                </div>
            </div>

            <div class="layui-form-item" style="margin-top: 35px">
                <div class="layui-input-block">
                    <span class="layui-btn"  lay-submit lay-filter="formSubmit" style="width: 100%">确认添加</span>
                </div>
            </div>
        </form>
    </div>
</div>
<script src="/assets/cos-js-sdk-v5/cos-js-sdk-v5.min.js"></script>
<script>

    layui.use(['form', 'upload'], function () {
        var $ = layui.jquery, form = layui.form, layer = layui.layer, upload = layui.upload;
        upload.render({
            elem: '#Upload',
            auto: false,
            accept: 'images',
            exts: 'svg|png|jpg|jpeg|gif|webpg',
            size: 1024 * 1024,
            choose: function (obj) {
                layer.load();
                var choosefile;
                obj.preview(function (index, file, result) {
                    choosefile = file;
                });

                $.ajax({
                    type: "POST", //请求方式
                    url: '/admin/props/signForCos', //请求地址
                    dataType: "json", //返回的数据类型
                    //请求成功
                    success: function (data) {
                        if (data.code == 0) {
                            var cos = new COS({
                                getAuthorization: function (options, callback) {
                                    var credentials = data.data.credentials;
                                    callback({
                                        TmpSecretId: credentials.tmpSecretId,
                                        TmpSecretKey: credentials.tmpSecretKey,
                                        XCosSecurityToken: credentials.sessionToken,
                                        ExpiredTime: data.data.expiredTime
                                    });
                                }
                            });
                            cos.putObject({
                                Bucket: data.data.bucket,
                                Region: data.data.region,
                                Key: 'images/' + data.data.filename + choosefile.name,
                                Body: choosefile,
                            }, function (err, res) {
                                layer.closeAll('loading');
                                if (err) {
                                    layer.msg("上传失败", {icon: 5});
                                } else {
                                    if (res.statusCode == 200) {
                                        layer.msg('上传成功', {icon: 1, time: 2000});
                                        $("#icon").val("https://" + res.Location);
                                    } else {
                                        layer.msg("图片上传失败", {icon: 5});
                                    }
                                }
                            });
                        } else {
                            layer.closeAll('loading');
                            layer.msg(data.msg, {icon: 5});
                        }
                    },
                    //请求失败
                    error: function (e) {
                        layer.closeAll('loading');
                        layer.msg('上传出现异常,请稍后再试', {icon: 2});
                    }
                });
            }
        });

        form.on('submit(formSubmit)', function(data){
            layer.confirm('确认要添加吗？', {icon: 6}, function () {
                $.ajax({
                    type: "POST", //请求方式
                    url: '/admin/props/add', //请求地址
                    dataType: "json", //返回的数据类型
                    data: $("#sbumitForm").serialize(), //数据
                    //请求成功
                    success: function (data) {
                        if (data.code == 0) {
                            layer.msg(data.msg, {icon: 1, time: 2000}, function () {
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                                window.parent.location.reload();
                            });
                        } else {
                            layer.msg(data.msg, {icon: 2, time: 5000});
                        }
                    },
                    //请求失败
                    error: function (e) {
                        if (e.responseJSON.msg) {
                            layer.msg(e.responseJSON.msg, {icon: 2});
                        }
                    }
                });
            });
        });
    });

    function icon_view() {
        if (document.getElementById('icon').value) {
            layer.photos({
                photos: {
                    "title": "",
                    "data": [{
                        "src": document.getElementById('icon').value,
                    }]
                }
                , shade: 0.01
                , closeBtn: 1
            });
        }
    }

</script>
</body>

</html>
